#coding:utf-8
import json
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from lovewith.account.views.modules.weddingapi import WeddingApi
from lovewith.api.qiniu import Qiniu
from lovewith.utils.image import MtImage
from lovewith.utils.upload import Upload
from lovewith.card.views.models.card_api import CardApi


#上传图片
def upload_image(request):
    attach = request.FILES.get('file', None)
    image_path = Upload().save_files(attach)

    return HttpResponse(image_path)


#更新请柬请柬
@login_required
def update_card(request):
    #新娘
    bride = request.POST.get('bride')
    #新郎
    bridegroom = request.POST.get('bridegroom')
    #日期
    date = request.POST.get('date')
    #时间
    time = request.POST.get('time')
    #地点
    address = request.POST.get('address')
    #用户角色
    user_role = int(request.POST.get('userRole') or 1)
    #地图坐标
    marker_point = request.POST.get('markerPoint')
    #邀请语
    content = request.POST.get('content')
    #请柬配置
    config = request.POST.get('config')
    #权限
    permission = int(request.POST.get('permission') or 2)
    #城市
    city = request.POST.get('city')
    result = {'error': True}

    #暂不验证请柬配置项是否正确
    if city and bride and bridegroom and date and time and address and marker_point:
        WeddingApi().update(
            bride=bride,
            bridegroom=bridegroom,
            wedding_date=date,
            wedding_time=time,
            city=city,
            address=address,
            user_role=int(user_role),
            user=request.user
        )
        if marker_point:
            card_id = CardApi().update(
                title=u'%s&%s的婚礼请柬' % (bridegroom, bride),
                map_point=marker_point,
                content=content,
                card_config=config,
                permission=permission,
                user=request.user
            )

        result = {'error': False, 'id': card_id}

    return HttpResponse(json.dumps(result))


#更新请柬标题
@login_required
def update_card_title(request):
    title = request.POST.get('title')
    card_data = CardApi.check_data(request.user.id)
    if title and card_data:
        card_data.title = title
        card_data.save()

    return HttpResponse(json.dumps({'error': False}))


#更新请柬内页
@login_required
def update_post(request):
    post_id = request.POST.get('id')
    title = request.POST.get('title') or ''
    content = request.POST.get('content')
    media = request.POST.get('media')
    x = request.POST.get('x')
    y = request.POST.get('y')
    #剪切模式
    crop_mode = request.POST.get('crop_mode')

    result = {'error': True}
    card_data = CardApi.check_data(request.user.id)

    if title and card_data:
        if media and x and y and crop_mode:
            if crop_mode == '4:3':
                img_width = 600
                img_height = 450
            else:
                img_width = 450
                img_height = 600

            crop_image = MtImage().create_thumb(
                media,
                img_width,
                img_height,
                crop_x=int(x),
                crop_y=int(y)
            )
            #保存图片到upyun
            new_crop_path = Qiniu('mt-card').upload(crop_image)

            new_media_post = CardApi.add_post_data(
                card=card_data,
                title=title,
                media=new_crop_path['path'],
                user=request.user
            )

            result = {'error': False, 'id': new_media_post.id, 'media': new_media_post.media}
        else:
            #保存文字
            if post_id:
                card_post_data = CardApi.check_post_data(post_id, request.user.id)
                if card_post_data:
                    card_post_data.title = title
                    card_post_data.content = content
                    card_post_data.save()

                    result = {'error': False, 'id': card_post_data.id}
            else:
                new_txt_post = CardApi.add_post_data(
                    card=card_data,
                    title=title,
                    content=content,
                    user=request.user
                )

                result = {'error': False, 'id': new_txt_post.id}

    return HttpResponse(json.dumps(result))


#删除内容
@login_required
def delete_post(request):
    post_id = request.GET.get('id')
    result = {'error': False}

    if post_id:
        card_post_data = CardApi.check_post_data(post_id, request.user.id)
        if card_post_data:
            card_post_data.delete()

            result = {'error': False}

    return HttpResponse(json.dumps(result))


#更新邀请人
@login_required
def update_invite(request):
    card_id = request.POST.get('card_id')
    invite_data = json.loads(request.POST.get('invite_data'))
    result = {'error': True}

    if card_id:
        card_data = CardApi.check_card(card_id)
        if card_data:
            for user in invite_data:
                user_email = user.get('email')
                user_name = user.get('name')
                user_id = user.get('uid')
                invite_type = user.get('type')
                is_send = user.get('send') == 1 and True or False

                invite_userdata = CardApi.check_invite_user(card_data.id, user_id=user_id, invite_type=invite_type)
                if invite_userdata:
                    #更新名称
                    invite_userdata.user_name = user_name
                    invite_userdata.is_send = is_send
                    invite_userdata.save()
                else:
                    CardApi.update_invite(
                        card=card_data,
                        user_id=user_id,
                        user_name=user_name,
                        user_email=user_email,
                        invite_type=invite_type,
                        is_send=is_send
                    )

            result = {'error': False}

    return HttpResponse(json.dumps(result))


#删除邀请人
@login_required
def delete_inviter(request):
    user_id = request.GET.get('uid')
    invite_type = request.GET.get('type')
    result = {'error': True}


    if user_id:
        card_data = CardApi.check_data(request.user.id)
        if card_data:
            invite_userdata = CardApi.check_invite_user(card_data.id, user_id=user_id, invite_type=invite_type)
            if invite_userdata:
                invite_userdata.delete()

                result = {'error': False}

    return HttpResponse(json.dumps(result))